package HOT;

/**
 * Author  zxy
 * Date  2023/6/19 下午 3:10
 */
public class LongestPalindrome {
    public String longestPalindrome(String s) {

        int maxm = 1;
        int start = 0;
        int len = s.length();

        boolean[][] dp = new boolean[len][len];

        for (int i = 0; i < len; i++) {
            dp[i][i] = true;
        }

        for (int i = 2; i <= len; i++) {
            for (int j = 0; j + i - 1 < len; j++) {
                int r = j + i - 1;
                if (s.charAt(j) == s.charAt(r)) {
                    if (i == 2 || dp[j + 1][r - 1]) {
                        dp[j][r] = true;
                        maxm = Math.max(maxm, i);
                        start = j;
                    }
                }
                else {
                    dp[j][r] = false;
                }
            }
        }

        return s.substring(start, start + maxm);

    }
}
